package com.course.android.activity.ac;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSONObject;
import com.baselib.android.cach.PreferceKey;
import com.baselib.android.cach.PreferceManager;
import com.baselib.android.utils.ApplicationUtil;
import com.baselib.android.utils.Logger;
import com.baselib.android.utils.ToastTool;
import com.course.android.R;
import com.course.android.activity.ActivityIntent;
import com.course.android.activity.VersionActivity;
import com.course.android.activity.base.BaseActivity;
import com.course.android.ui.adapter.HomePageAdaper;
import com.course.android.ui.adapter.MenuAdapter;
import com.course.controller.UserController;
import com.course.controller.VersionController;
import com.course.model.HttpConfigBean;
import com.course.model.resp.UserVo;
import com.course.model.resp.Version;
import com.course.utils.web.HttpRespCallBack;
import com.google.android.material.tabs.TabLayout;

import org.apache.commons.lang.StringUtils;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends BaseActivity {
    TabLayout tabLayout;
    HomePageAdaper pagerAdapter;
    Toolbar mtoolbar;

    TextView usernameTv;
    NavigationView navigationView;

    ListView menuLv;
    MenuAdapter adapter;
    @Override
    protected void initView() {
        DrawerLayout drawer =findViewById(R.id.drawer_layout);


        //抽屉菜单
        navigationView=findViewById(R.id.nav_view);
        usernameTv = navigationView.getHeaderView(0).findViewById(R.id.usernameTv);
        menuLv = navigationView.getHeaderView(0).findViewById(R.id.menuLv);
        adapter = new MenuAdapter(this);
        menuLv.setAdapter(adapter);
        menuLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToastTool.show(HomeActivity.this,"待开发");
            }
        });

        List<String> menus = new ArrayList<>();
        menus.add("我的收藏");
        menus.add("我的评论");
        menus.add("设置");
        adapter.setRes(menus);
        //-----------------------------------------


        //bar
        mtoolbar=findViewById(R.id.toolbar);
        setSupportActionBar(mtoolbar);
        mtoolbar.setNavigationIcon(R.drawable.drawer_icon);
        mtoolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawer.open();
            }
        });
        //-----------------------------------------
        
        //-----------------------------------------tab  pager   组合

        ViewPager viewPager=findViewById(R.id.fragmentcontainer);
        tabLayout=findViewById(R.id.include);
        pagerAdapter=new HomePageAdaper(getSupportFragmentManager(),4);
        viewPager.setAdapter(pagerAdapter);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                pagerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//        usernameTv


    }

    @Override
    protected void initData() {
        VersionController.getInstance().get(ApplicationUtil.getVersionCode(this), new HttpRespCallBack() {
            @Override
            public void onReqStart(HttpConfigBean flag) {

            }

            @Override
            public void onReqFinish(HttpConfigBean flag) {

            }

            @Override
            public void onReqFail(HttpConfigBean flag, Exception e, String errorMsg) {

            }

            @Override
            public void onSuccess(HttpConfigBean flag, boolean isPage, Object o) {
                Version version = JSONObject.parseObject(o.toString(), Version.class);
                if (version.getNeedUpdate()==1){
                    Intent intent = new Intent(HomeActivity.this, VersionActivity.class);
                    intent.putExtra("m",version);
                    startActivity(intent);
                }

            }
        });
    }

    @Override
    protected int bindLayout() {
        return R.layout.activiy_home;
    }

    @Override
    protected void onActivityResultCallBack(Integer reqCode, Intent data) {

    }

    void checkAndBindLogin(){

        usernameTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserController.getInstance().isLogin(HomeActivity.this)){
                    ActivityIntent.toLoginsIntent(newIntentLauncher(),HomeActivity.this);
                }
            }
        });
        if (UserController.getInstance().isLogin(HomeActivity.this)){
            String userInfo = PreferceManager.getInsance().getValueBYkey(this, PreferceKey.USER_INFO);
            UserVo userVo = JSONObject.parseObject(userInfo,UserVo.class);
            usernameTv.setText(userVo.getName());
        }else {
            usernameTv.setText("点击登录");
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        checkAndBindLogin();
    }
}
